package com.cn.longdistancebusstation.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.callback.OnCouponListener;
import com.cn.longdistancebusstation.callback.OnRecyclerViewItemClickListener;
import com.cn.longdistancebusstation.enumxx.PassengerType;
import com.cn.longdistancebusstation.model.Contacter;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mucheng on 2018/2/1.
 */

public class PassengersOfConfirmOrderAdapter extends BaseAdapter{
    private Context ctx;
    private List<Contacter> mContacterList;
    private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;
    private OnCouponListener mOnCouponListener;
    private BigDecimal mCoupon;
    //是否支持送站服务
    private boolean mIsSupportSendService;
    public static Map<Integer, Boolean> isSelected;
    public PassengersOfConfirmOrderAdapter(Context ctx, List<Contacter> list, boolean isSupportSendService){
        this.ctx=ctx;
        this.mContacterList =list;
        this.mIsSupportSendService = isSupportSendService;
        init();
    }

    private void init() {
        isSelected = new HashMap<Integer, Boolean>();
        for (int i = 0; i < mContacterList.size(); i++){
            isSelected.put(i, false);
        }
    }

    @Override
    public int getCount() {
        return mContacterList.size();
    }

    @Override
    public Object getItem(int i) {
        return mContacterList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        final int pos = i;
        if(view == null){
            viewHolder = new ViewHolder();
            view= LayoutInflater.from(ctx).inflate(R.layout.view_payment_order,null);
            viewHolder.mNameTextView =(TextView)view.findViewById(R.id.confirmOrder_list_item_name_textView);
            viewHolder.mCardNoTextView =(TextView)view.findViewById(R.id.confirmOrder_list_item_cardNo_textView);
            viewHolder.mImageView = (ImageView)view.findViewById(R.id.confirmOrder_list_item_delete_imageView);
            viewHolder.mCouponTextView=(TextView)view.findViewById(R.id.confirmOrder_list_item_coupon_textView);
            viewHolder.mSendCheckBox=(CheckBox)view.findViewById(R.id.send_checkBox);
            viewHolder.mSendTextView=(TextView)view.findViewById(R.id.textView_send);
            view.setTag(viewHolder);
        }
        viewHolder=(ViewHolder)view.getTag();
        final Contacter contacter= mContacterList.get(i);
        //
        String strPassengerType = PassengerType.getPassengerType(contacter.getType()).desc;
        viewHolder.mNameTextView.setText(contacter.getName() + "(" + strPassengerType + ")");
        //
        viewHolder.mCardNoTextView.setText(contacter.getCardNo());
        //
        viewHolder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnRecyclerViewItemClickListener.onClick(pos);
            }
        });
        //
        viewHolder.mCouponTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnRecyclerViewItemClickListener.onClickCoupon(pos);
            }
        });
        viewHolder.mSendCheckBox.setChecked(contacter.getSendChoose()==null ? false : contacter.getSendChoose());
        viewHolder.mSendCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                contacter.setSendChoose(b);
            }
        });
        //判断乘客是否是成年人，只有成年才可以使用优惠券，其他身份不可选择,直接隐藏
//        if(PassengerType.getPassengerType(contacter.getType()) == PassengerType.PASSENGER_ADULT){
//            viewHolder.mCouponTextView.setVisibility(View.VISIBLE);
//        }
//        else{
//            viewHolder.mCouponTextView.setVisibility(View.INVISIBLE);
//        }
        if(contacter.getPrivilege().getDiscount().equals("0")){
            viewHolder.mCouponTextView.setText("优惠券");
        }
        else{
            String strDiscount = contacter.getPrivilege().getDiscount();
            BigDecimal decimal = new BigDecimal(strDiscount);
            String strCoupon =decimal.multiply(new BigDecimal(10)).setScale(1).toString();
            viewHolder.mCouponTextView.setText("优惠券:" + strCoupon + " 折");
        }
        if(mIsSupportSendService){
            viewHolder.mSendCheckBox.setVisibility(View.VISIBLE);
            viewHolder.mSendTextView.setVisibility(View.VISIBLE);
        }else{
            viewHolder.mSendCheckBox.setVisibility(View.GONE);
            viewHolder.mSendTextView.setVisibility(View.GONE);
        }
        return view;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        mOnRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public class ViewHolder{
        TextView mNameTextView;
        TextView mCardNoTextView;
        TextView mCouponTextView;
        ImageView mImageView;
        public CheckBox mSendCheckBox;
        TextView mSendTextView;
    }
}
